package com.dycx.user.converter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.user.dto.CommentDTO;
import com.dycx.user.entity.CommentDO;
import com.dycx.user.enums.CommentStatusEnum;
import com.dycx.user.vo.CommentVO;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 评论(Comment)实体转换类
 *
 * @author wuzongbin
 * @since 2023-12-29 10:53:26
 */


@Data
public class CommentConverter implements Serializable {
    public static CommentDO dtoToEntity(CommentDTO dto) {
        if (dto == null) return null;
        CommentDO entity = BeanUtil.toBean(dto, CommentDO.class);
        return entity;
    }

    public static CommentVO entityToVo(CommentDO entity) {
        if (entity == null) return null;
        CommentVO vo = BeanUtil.toBean(entity, CommentVO.class);
        vo.setBizTypeName(BizTypeEnum.converter(vo.getBizType()));
        vo.setStatusName(CommentStatusEnum.converter(vo.getStatus()));

        return vo;
    }

    public static List<CommentVO> entityToVoList(List<CommentDO> entityList) {
        List<CommentVO> voList = new ArrayList<>();
        for (CommentDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<CommentVO> entityToVoPage(Page<CommentDO> page) {

        List<CommentVO> newRecords = new ArrayList<>();

        List<CommentDO> records = page.getRecords();
        for (CommentDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<CommentVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
